<?php

class MobileFoodController extends MobileController {

    static $model_name = 'food';
    static $display_name = '餐品';
	/*
	|--------------------------------------------------------------------------
	| Default Home Controller
	|--------------------------------------------------------------------------
	|
	| You may wish to use controllers instead of, or in addition to, Closure
	| based routes. That's great! Here is an example controller method to
	| get you started. To route to this controller, just add the route:
	|
	|	Route::get('/', 'HomeController@showWelcome');
	|
	*/

	public function getIndex()
	{

        $result =  $this->getService()->getList(array(), 8);
        $entries = $result['entries'];
        $links = $result['links'];

        $menu_id = Input::get( 'menu_id', null );
        $title = '全部餐品';
        if(!empty($menu_id)){
            $title = Menu::where('id', $menu_id)->select('name')->get()->first()->name;
        }
        $menus = Menu::select('id','name')->get();

        return View::make(self::$view_path.'.'.self::$model_name.'.list', compact('entries','menus', 'title', 'links'));
	}

    public function getTodayFoods(){
        $input['updated_at_start'] = date('Y-m-d');
        $result =  $this->getService()->getList($input);
        $entries = $result['entries'];
        $links = $result['links'];

        $title = '今日菜单';

        return View::make(self::$view_path.'.'.self::$model_name.'.list', compact('entries','title', 'links'));
    }


    public function getDetail($entry){
        return View::make(self::$view_path.'.food.detail', compact('entry'));
    }

    public function doOrder($entry){
        $service = $this->getService();
        if($this->isSubmit()){
            if(!$service->updateFood($entry)){
                return Redirect::action('BackFoodController@doUpdate', array($entry->id))->withInput()->with('errors',$this->service->errors());
            }else{
                return Redirect::action('BackFoodController@doUpdate', array($entry->id))->with('success','修改'.self::$display_name.'成功');
            }
        }

        return View::make(self::$view_path.'.'.self::$model_name.'.create_update', compact('entry', 'permissions'));
    }

    /**
     * Remove the specified user from storage.
     *
     * @param $entry
     * @internal param $id
     * @return Response
     */
    public function cancelOrder($entry)
    {
        $service = $this->getService();

        if(!$service->deleteFood($entry)) {
            return Redirect::action('BackFoodController@getIndex')->with('error',$this->service->errors()->all());

        }
        return Redirect::action('BackFoodController@getIndex')->with('success', '删除'.self::$display_name.'成功');
    }


    public function getService() {
        if(is_null($this->service)){
            $this->service = new MobileFoodService();
        }
        return $this->service;
    }

}
